/**
 * Created by Administrator on 2019/5/22.
 */
var sysurl = "http://wsc.tangmu.xin/api/";
var imgurl = "http://wsc.tangmu.xin";
var rootLink = document.location.toString();
var tmpsUrls = rootLink.split("www/")[0];
var data = {}
var postCfg = {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    transformRequest: function (data) {
        return $.param(data);
    }
};




// axios.defaults.baseURL = sysurl;
// axios.defaults.headers.post['Content-Type'] = 'application/json';
//
// Vue.config.productionTip = false

/**
 * 设置cookie
 * @param {type} name
 * @param {type} value
 * @returns {undefined}
 */
function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
}

/**
 * 获取cookie
 * @param {type} name
 * @returns {unresolved}
 */
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    }
    return null;
}

/**
 * 删除cookie
 * @param {type} name
 * @returns {undefined}
 */
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval !== null)
        document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
}

/**
 * 获取链接参数
 * @param {type} name
 * @returns {unresolved}
 */
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r !== null)
        return  unescape(r[2]);
    return null;
}



function timeinit() {
    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
    var checkin = $('#dpd1').fdatepicker({
        format: 'yyyy-mm-dd',
        onRender: function (date) {
            // return date.valueOf() < now.valueOf() ? 'disabled' : '';
            return '';
        }
    }).on('changeDate', function (ev) {
        if (ev.date.valueOf() > checkout.date.valueOf()) {
            var newDate = new Date(ev.date)
            newDate.setDate(newDate.getDate() + 1);
            checkout.update(newDate);
        }
        checkin.hide();
        $('#dpd2')[0].focus();
    }).data('datepicker');
    var checkout = $('#dpd2').fdatepicker({
        format: 'yyyy-mm-dd',
        onRender: function (date) {
            return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
        }
    }).on('changeDate', function (ev) {
        checkout.hide();
    }).data('datepicker');


    var checkover = $('#dpd3').fdatepicker({
        format: 'yyyy-mm-d',
        onRender: function (date3) {
            return date3.valueOf() < now.valueOf() ? 'disabled' : '';
        }
    }).on('changeDate', function (ev) {
        checkover.hide();
    }).data('datepicker');


    var checkin2 = $('#dpd4').fdatepicker({
        format: 'yyyy-mm-dd',
        onRender: function (date) {
            // return date.valueOf() < now.valueOf() ? 'disabled' : '';
            return '';
        }
    }).on('changeDate', function (ev) {
        if (ev.date.valueOf() > checkout2.date.valueOf()) {
            var newDate = new Date(ev.date)
            newDate.setDate(newDate.getDate() + 1);
            checkout2.update(newDate);
        }
        checkin2.hide();
        $('#dpd5')[0].focus();
    }).data('datepicker');
    var checkout2 = $('#dpd5').fdatepicker({
        format: 'yyyy-mm-dd',
        onRender: function (date) {
            return date.valueOf() <= checkin2.date.valueOf() ? 'disabled' : '';
        }
    }).on('changeDate', function (ev) {
        checkout2.hide();
    }).data('datepicker');
}


axios.defaults.retry = 4;
axios.defaults.retryDelay = 1000;
var index;
//添加请求拦截器
axios.interceptors.request.use(config => {
    //在发送请求之前做某事，比如说 设置loading动画显示
    index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
    return config
}, error => {
    //请求错误时做些事
    return Promise.reject(error)
})

axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
    var config = err.config;
    // If config does not exist or the retry option is not set, reject
    if(!config || !config.retry) return Promise.reject(err);

    // Set the variable for keeping track of the retry count
    config.__retryCount = config.__retryCount || 0;

    // Check if we've maxed out the total number of retries
    if(config.__retryCount >= config.retry) {
        // Reject with the error
        return Promise.reject(err);
    }

    // Increase the retry count
    config.__retryCount += 1;

    // Create new promise to handle exponential backoff
    var backoff = new Promise(function(resolve) {
        setTimeout(function() {
            resolve();
        }, config.retryDelay || 1);
    });

    // Return the promise in which recalls axios to retry the request
    return backoff.then(function() {
        return axios(config);
    });
});

//添加响应拦截器
axios.interceptors.response.use(response => {
    //对响应数据做些事，比如说把loading动画关掉
        layer.close(index);
    if(response.data.code == 205){
            setTimeout(function () {
                delCookie("token");
                window.location.href='login.html';
            }, 3000);



    }
    return response
}, error => {
    //请求错误时做些事
    layer.close(index);
    return Promise.reject(error)
})

$(document).on('click','td img',(function (e) {
    //点击预览图片
    var src = e.target.src;
    layer.open({
        type:1
        ,title:false
        ,closeBtn:0
        ,area: ['750px', '500']
        ,skin:'layui-layer-nobg'
        ,shadeClose:true
        ,content:'<img src="'+src+'" style="max-height:600px;max-width:100%;object-fit: contain">'
        ,scrollbar:false
    })


}))



